<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <button type="primary" class="btn-login" @click="getUserInfo">一键登录</button>
    <text class="tips-text">登陆后尽享更多权益</text>
  </view>
</template>

<script>
  import {mapState,mapMutations} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    computed:{
      ...mapState('m_user',['userinfo','redirectInfo'])
    },
    methods:{
      ...mapMutations('m_user',['updateUserInfo','updateToken','updateRedirectInfo']),
      getUserProfile(e){
         // @chooseavatar="getUserProfile"
        console.log(e);
      },
      getUserInfo(e){
      //判断是否获取用户信息成功
        // if(e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('你取消了登录授权！')
        //获取用户信息成功e.detail.userinfo就是用户的基本信息
         // console.log('微信授权弹窗');
            uni.getSetting({ 
                success:(res) =>{ 
                    // console.log("授权：", res);
                    uni.showModal({ 
                        title: '授权提醒',
                        content: '请登录，以便为您提供更好的服务',
                        cancelText: "取消授权",
                        confirmText: '确认授权',
                        success:  (res)=> {
                          if(res.confirm){
                            uni.getUserProfile({
                              desc:'登录',
                              success:(res)=> {
                                this.updateUserInfo(res.userInfo)
                                uni.login({
                                  success:(code)=> {
                                    if(code.errMsg!="login:ok") {
                                      return this.$showMsg('登录失败')
                                    }
                                    this.getToken(res,code.code)
                                  }
                                })
                            }
                          })
                          }else if(res.cancel){
                            uni.$showMsg('取消授权')
                          }
                            } 
                        })
                    }
                })
      },
       getToken(info,c){
        // console.log(info.encryptedData);
        // console.log(c);
        // const query = {
        //   code:c,
        //   encryptedData: ,
        //   iv: "info.ivkkkkkkkkkkkkm",
        //   rawData: "info.rawDatallllllkkkkkkk",
        //   signature: "info.signaturekkkkkkkk"
        // }
        // const {data:res} = await uni.$http.post('/api/public/v1/users/wxlogin',query)
        // console.log(res);
        // if(res.meta.status!=200) return uni.$showMsg('登录失败！')
        // uni.$showMsg('登录成功！')
        let tk = "BearereyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
        this.updateToken(tk)
        uni.$showMsg('登录成功！')
        this.navigateBack()
      },
      // 返回登录之前的页面
      navigateBack(){
        if(this.redirectInfo&&this.redirectInfo.opentype=='switchTab'){
          console.log(this.redirectInfo);
          uni.switchTab({
            url:this.redirectInfo.from,
            // 导航成功之后，把 vuex 中的 redirectInfo 对象重置为 null
            success: () => {
              this.updateRedirectInfo(null)
              uni.$showMsg('登录成功')
            }
          })
        }
      }
      
    }
  }
</script>

<style lang="scss">
.login-container{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-top: 20%;
  .btn-login{
    width: 90%;
    margin: 15px 0;
    border-radius: 30px
  }
  .tips-text{
    font-size: 11px;
    color: gray;
  }
}
</style>